package com.talentech.xproberlib;

import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import static com.talentech.xproberlib.Utils.log;
import static com.talentech.xproberlib.Utils.simpleObjectDescribe;

public class ActivityHooks
{
	public static final String ACTIVITY_STARTER = "XPROBER_ACTIVITY_STARTER";

	public static XC_MethodHook startActivityHook = new XC_MethodHook()
	{
		protected void beforeHookedMethod(MethodHookParam param) throws Throwable
		{
			if (!param.method.getName().equals("startActivityForResult"))
			{
				log("Incorrect hook point");
				return;
			}

			if (param.args[0] == null)
			{
				return;
			}

			Intent intent = (Intent) param.args[0];
//			List<ResolveInfo> activityList = ((Activity) param.thisObject).getPackageManager().queryIntentActivities(intent, 0xffffffff);
//
//			log("Activity to start: ");
//			for (ResolveInfo info : activityList)
//			{
//				String activityName = info.activityInfo.targetActivity;
//				if (activityName != null)
//				{
//					log(" " + activityName);
//				}
//			}

			intent.putExtra(ACTIVITY_STARTER, simpleObjectDescribe(param.thisObject));
		}

		protected void afterHookedMethod(MethodHookParam param) throws Throwable
		{

		}
	};

	public static XC_MethodHook onCreateHook = new XC_MethodHook()
	{
		protected void beforeHookedMethod(MethodHookParam param) throws Throwable
		{
			if (!param.method.getName().equals("onCreate"))
			{
				log("Incorrect hook point");
				return;
			}

			Intent intent = ((Activity) param.thisObject).getIntent();
			if (intent == null)
			{
				log("Null intent");
				return;
			}

			String starter = intent.getStringExtra(ACTIVITY_STARTER);
			if (starter == null)
			{
				log("starter == null");
				return;
			}
			log("Activity started by: " + starter);
		}

		protected void afterHookedMethod(MethodHookParam param) throws Throwable
		{

		}
	};
}
